/*
 * @Description: 
 * @Author: your name
 * @Date: 2024-09-09 21:16:16
 * @version: 
 * @LastEditors: your name
 * @LastEditTime: 2024-09-12 16:36:13
 */
import * as Cesium from 'cesium';
// 自定义 WallGradientsMaterialProperty
class WallGradientsMaterialProperty {
    constructor(color) {
        this._definitionChanged = new Cesium.Event();
        this._color = undefined;
        this._colorSubscription = undefined;
        this.color = color;
    }
    get isConstant() {
        return false;
    }
    get definitionChanged() {
        return this._definitionChanged;
    }

    getType(time, result) {
        return 'WallGradients';
    };

    getValue(time, result) {
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
        result.image = Cesium.Material.WallGradientsImage;
        return result;
    };

    equals(other) {
        return( 
            this === other || 
            (other instanceof WallGradientsMaterialProperty && 
                Cesium.Property.equals(this._color, other._color))
        );
    };
}

// 对于color，我们需要包装成property
Object.defineProperties(WallGradientsMaterialProperty.prototype, {
    color: Cesium.createPropertyDescriptor("color")
});


// 注册自定义材质
Cesium.Material.WallGradientsType = 'WallGradients';
Cesium.Material.WallGradientsImage =  "/src/assets/materialResources/wallgradients.png";
Cesium.Material.WallGradientsSource =
    /*glsl*/`czm_material czm_getMaterial(czm_materialInput materialInput)
    {
        czm_material material = czm_getDefaultMaterial(materialInput);
        vec2 st = materialInput.st;
        vec4 colorImage = texture2D(image, vec2(fract(st.t - time), st.t));
        float time=fract(czm_frameNumber/10.0);
        float isAlpha=step(0.5,time);
        float dis = distance(st, vec2(0.5));
        if(isAlpha>=1.0){
           material.alpha = colorImage.a * color.a* dis *2.5;
       }else{
           material.alpha = colorImage.a * color.a * dis *2.0;
       }
        material.diffuse =  2.5 * color.rgb  ;
        return material;
     }`;

Cesium.Material._materialCache.addMaterial(Cesium.Material.WallGradientsType, {
    fabric: {
        type: Cesium.Material.WallGradientsType,
        uniforms: {
            color: new Cesium.Color(1.0, 1.0, 1.0, 0.5),
            image: Cesium.Material.WallGradientsImage,
            time: 0
        },
        source: Cesium.Material.WallGradientsSource
    },
    translucent: function (material) {
        return true;
    }
});

export default WallGradientsMaterialProperty;